package com.xinqi.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ParamInterceptor implements HandlerInterceptor {
 
 
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String token = httpServletRequest.getHeader("token");
        //token验证
        if(token == null){
            System.out.println("token验证失败");
            httpServletResponse.setStatus(401);
            return false;
        }

        String id = stringRedisTemplate.opsForValue().get(token);
        if (id == null){
            System.out.println("token验证失败");
            httpServletResponse.setStatus(401);

            return false;
        }

        System.out.println("token验证成功");
        return true;

    }

 
}
